package io.lckgroup.modules.lck.controller.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.ShopFoodTypeEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.entity.MerchantUserEntity;
import io.lckgroup.modules.lck.service.ShopFoodTypeService;
import io.lckgroup.modules.lck.service.ShopMerchantInfoService;
import io.lckgroup.modules.lck.service.MerchantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP Do菜品分类表
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-01-19 19:14:39
 */
@RestController
@RequestMapping("/app/foodType")
@Api("APP登录接口")
public class AppShopFoodTypeController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ShopFoodTypeService shopFoodTypeService;

    /**
     * 添加修改菜品分类信息
     *
     * @param entity
     * @param sm
     * @return R
     */
    @AppLogin
    @PostMapping("addFoodType")
    @ApiOperation("添加修改菜品分类信息")
    @CacheEvict(value = {"findFoodList", "selectPageByAuditState"}, allEntries = true)
    public R addFoodType(@RequestBody ShopFoodTypeEntity entity, @AppLoginUser ShopMerchantInfoEntity sm) {
        boolean result = false;
        Long id = entity.getId();

        //判断是否有重复数据
        String newName = entity.getTypeName().replaceAll(" ", "");
        entity.setTypeName(newName);

        if (null == id) {
            //新增数据
            //获取商户当前登录用户的商户信息
            ShopMerchantInfoEntity merchant = sm;
            //设置商户ID(获取商户信息，来源：前台或者后台)
            entity.setMerchantId(merchant.getId());
            //entity.setGmtCreate(new Date());
            entity.setFoodNum(0);
            //通过菜品分类名称查询当前商家的数据
            ShopFoodTypeEntity query = new ShopFoodTypeEntity();
            query.setTypeName(newName);
            query.setMerchantId(sm.getId());
            Wrapper<ShopFoodTypeEntity> queryWrapper = new QueryWrapper<>(query);
            List<ShopFoodTypeEntity> list = shopFoodTypeService.list(queryWrapper);

            if (null != list && list.size() > 0) {
                return R.error(-1, "菜品分类已经存在");
            }
            //比对并返回结果
            result = shopFoodTypeService.save(entity);
        } else {
            //修改数据
            entity.setGmtModified(new Date());
            //通过菜品分类名称,商家id,查询除了自己以外的数据

            Wrapper<ShopFoodTypeEntity> queryWrapper =
                    new QueryWrapper<ShopFoodTypeEntity>()
                            .eq("merchant_id", sm.getId())
                            .eq("type_name", newName)
                            .notIn("id", id);
            List<ShopFoodTypeEntity> list = shopFoodTypeService.list(queryWrapper);

            if (null != list && list.size() > 0) {
                return R.error(-1, "菜品分类已经存在");
            }
            result = shopFoodTypeService.updateById(entity);
        }
        if (result) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "操作失败");
        }
    }

    /**
     * 获取菜品分类的列表
     *
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getList")
    @ApiOperation("获取菜品分类的列表")
    public R getList(@AppLoginUser ShopMerchantInfoEntity sm) {
        //获取商家id
        ShopMerchantInfoEntity merchant = sm;
        Long id = merchant.getId();
        //获取商家的列表信息
        ShopFoodTypeEntity query = new ShopFoodTypeEntity();
        query.setMerchantId(id);
        Wrapper<ShopFoodTypeEntity> queryWrapper = new QueryWrapper<>(query);
        List<ShopFoodTypeEntity> list = shopFoodTypeService.list(queryWrapper);
        if (list.size() > 0) {
            return R.ok().put("results", list);
        } else {
            return R.ok().put("results", null);
        }

    }

    /**
     * 获取菜品分类的列表
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/getFoodTypeById/{id}")
    @ApiOperation("获取菜品分类的列表")
    public R getFoodTypeById(@PathVariable("id") String id) {
        ShopFoodTypeEntity entity = shopFoodTypeService.getById(id);
        if (null != entity) {
            return R.ok().put("results", entity);
        } else {
            return R.ok().put("results", null);
        }

    }

    /**
     * 通过主键删除菜品分类信息
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/deleteFoodTypeById/{id}")
    @ApiOperation("通过主键删除菜品分类信息")
    @CacheEvict(value = {"findFoodList", "selectPageByAuditState"}, allEntries = true)
    public R deleteFoodTypeById(@PathVariable("id") String id) {
        ShopFoodTypeEntity entity = new ShopFoodTypeEntity();
        entity.setId(Long.parseLong(id));
        entity.setFoodNum(0);
        Wrapper<ShopFoodTypeEntity> queryWrapper = new QueryWrapper<ShopFoodTypeEntity>(entity);
        boolean result = shopFoodTypeService.remove(queryWrapper);
        if (result) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "只能删除没有菜品的分类");
        }

    }

}
